<?php
class Manage_follow extends CI_Model {

    function __construct(){
        parent::__construct();
    }
    function follows($user_id,$following){
        if($this->isFollowing($user_id,$following)){
            return false;
        }
        $data = array(
          'user_id' => $user_id,
        );
        $follow = array(
          'follower' => $user_id,
          'following' => $following
        );
        $this->mongo_db->insert('follows' ,$follow);
        return $this->mongo_db->where($data)->push("follow", $following)->update('community');
        
    }
    function unFollows($user_id,$following){
        $data = array(
          'user_id' => $user_id,
        );
        $follow = array(
          'follower' => $user_id,
          'following' => $following
        );
        $this->mongo_db->where($follow)->delete('follows');
        return $this->mongo_db->where($data)->pull("follow", $following)->update('community');
        
    }
    function getFollowing($user_id){
        $data = array(
          'user_id' => $user_id,
        );
        $user = $this->mongo_db->where($data)->get('community');
        if(count($user) == 0){
            return false;
        }
        return $user[0]['follow'];
    }
    function isFollowing($user_id,$following){
        if(!$this->getFollowing($user_id))
            return false;
        $follow_arr = $this->getFollowing($user_id);
        if(in_array($following, $follow_arr)){
            return true;
        }else{
            return false;
        }
    }
}
?>
